package com.dyw.spzx.manager.controller;


import com.dyw.spzx.manager.service.SysMenuService;
import com.dyw.spzx.manager.service.SysUserService;
import com.dyw.spzx.manager.service.ValidateCodeService;
import com.dyw.spzx.model.dto.system.LoginDto;
import com.dyw.spzx.model.vo.system.SysMenuVo;
import com.dyw.spzx.model.entity.system.SysUser;
import com.dyw.spzx.model.vo.common.Result;
import com.dyw.spzx.model.vo.common.ResultCodeEnum;
import com.dyw.spzx.model.vo.system.LoginVo;
import com.dyw.spzx.model.vo.system.ValidateCodeVo;
import com.dyw.spzx.util.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "首页接口")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private SysMenuService sysMenuService;



    @Operation(summary = "动态菜单")
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList() ;
        return Result.ok(sysMenuVoList);
    }


    // 用户登录
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.ok(loginVo);
    }

    // generateValidateCode 生成验证码
    @Operation(summary = "生成验证码")
    @GetMapping("generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.ok(validateCodeVo);
    }

    //获取用户信息
   /* @Operation(summary = "获取用户信息")
    @GetMapping("getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token) {
        SysUser sysUser = sysUserService.getUserInfo(token);
        return Result.ok(sysUser);
    }*/

    //获取用户信息
    @Operation(summary = "获取用户信息")
    @GetMapping("getUserInfo")
    public Result<SysUser> getUserInfo() {
        SysUser sysUser = AuthContextUtil.get();
        return Result.ok(sysUser);
    }


    // 退出登录
    @Operation(summary = "退出登录")
    @Parameters(value = {@Parameter(name = "token参数")})
    @GetMapping("logout")
   public Result logout(@RequestHeader("token") String token){
        sysUserService.logout(token);
        return Result.ok(ResultCodeEnum.SUCCESS);
   }

}
